﻿//  Copyright (c) 2011 Ray Liang (http://www.dotnetage.com)
//  Licensed MIT: http://www.opensource.org/licenses/mit-license.php
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DNA.Mvc.Data.Entity
{
    /// <summary>
    /// Implements ISubscriptionRepository with EF. 
    /// </summary>
    public class SubscriptionRepository : EntityRepository<Subscription>, ISubscriptionRepository
    {
        public SubscriptionRepository() : base() { }

        public SubscriptionRepository(DB dbContext) : base(dbContext) { }

        /// <summary>
        /// Remove the user from subscription list.
        /// </summary>
        /// <param name="token">Specified the subscription list token.</param>
        /// <param name="userName">Specified the user name who to unsubscribe.</param>
        public void Unsubscribe(string token, string userName)
        {
            if (string.IsNullOrEmpty(token)) throw new ArgumentNullException("token");

            if (string.IsNullOrEmpty(userName)) throw new ArgumentNullException("userName");

            if (IsSubscribed(token, userName))
            {
                var hashCode = token.GetHashCode();
                Delete(s => s.HashKey.Equals(hashCode) && s.UserName.Equals(userName));
                if (IsOwnContext)
                    Context.SaveChanges();
            }
        }

        /// <summary>
        /// Add user to the subscription list.
        /// </summary>
        /// <param name="token">Specified the subscription list token.</param>
        /// <param name="userName">Specified the user name who to subscribe.</param>
        /// <returns>Returns true when added user to subscription list.</returns>
        public bool Subscribe(string token, string userName)
        {
            if (string.IsNullOrEmpty(token)) throw new ArgumentNullException("token");

            if (string.IsNullOrEmpty(userName)) throw new ArgumentNullException("userName");

            if (!IsSubscribed(token, userName))
            {
                Create(new Subscription()
                     {
                         UserName = userName,
                         Token = token,
                         HashKey = token.ToLower().GetHashCode()
                     });
                if (IsOwnContext)
                    Context.SaveChanges();

                return true;
            }
            return false;
        }

        /// <summary>
        /// Gets/Sets whether the user is in the subscription list.
        /// </summary>
        /// <param name="token">Specified the subscription list token.</param>
        /// <param name="userName">Specified the user name who to subscribe.</param>
        /// <returns>Returns true when user is in subscription list.</returns>
        public bool IsSubscribed(string token, string userName)
        {
            if (string.IsNullOrEmpty(token)) throw new ArgumentNullException("token");
            if (string.IsNullOrEmpty(userName)) throw new ArgumentNullException("userName");

            var hashCode = token.GetHashCode();
            return DbSet.Count(s => s.HashKey.Equals(hashCode) && s.UserName.Equals(userName)) > 0;
        }

        /// <summary>
        /// Gets a subscription list by specified token.
        /// </summary>
        /// <param name="token">Specified the subscription list token.</param>
        /// <returns>Returns user name arrary.</returns>
        public string[] GetSubscribers(string token)
        {
            if (string.IsNullOrEmpty(token)) throw new ArgumentNullException("token");
            var hashCode = token.GetHashCode();
            return DbSet.Where(s => s.HashKey.Equals(hashCode)).Select(s => s.UserName).ToArray();
        }
    }
}
